package cn.com.nr.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasStationDeptEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasStationDeptService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.nr.entity.NrConcreteEndangerEntity;
import cn.com.nr.service.NrConcreteEndangerService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

/**
 * 分析矿山信息
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2025-02-08 16:34:40
 */
@Controller
@RequestMapping("/nr/concreteEndanger")
public class NrConcreteEndangerController {

	@Autowired
	private NrConcreteEndangerService nrConcreteEndangerService;
	@Autowired
	private GasStationinfoService gasStationinfoService;
	@Autowired
	private HttpSessionService httpSessionService;
	@Autowired
	private GasStationDeptService gasStationDeptService;
	@Autowired
	private DeptService deptService;
	
	@ApiOperation(value = "确认分析")
	@PostMapping("/realDelete")
	@ResponseBody
	public DataResult realDelete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		for (String id : ids) {
			if (TsBqUtils.isNotNull(id)) {
				nrConcreteEndangerService.realRemoveById(id);
				gasStationinfoService.realRemoveById(id);
			}
		}
		return DataResult.success();
	}
	
	
	@ApiOperation(value = "确认分析")
	@PostMapping("/add")
	@ResponseBody
	public DataResult add(@RequestBody @ApiParam(value = "id集合") List<String> ids) {

		for (String id : ids) {
			NrConcreteEndangerEntity entity = new NrConcreteEndangerEntity();
			GasStationinfoEntity gasStationinfoEntity = gasStationinfoService.getById(id);
			BeanUtils.copyProperties(gasStationinfoEntity, entity);
			entity.setUpdateTime(new Date());
			entity.setUpdateId(httpSessionService.getCurrentUserId());
			
			nrConcreteEndangerService.saveOrUpdate(entity);
		}
		return DataResult.success();
	}

	@ApiOperation(value = "取消分析")
	@PostMapping("/delete")
	@ResponseBody
	public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		for (String id : ids) {
			if (TsBqUtils.isNotNull(id)) {
				nrConcreteEndangerService.realRemoveById(id);
			}
		}

		return DataResult.success();
	}

	@ApiOperation(value = "编辑")
	@PutMapping("/update")
	@ResponseBody
	public DataResult update(@RequestBody NrConcreteEndangerEntity nrConcreteEndanger) {
		String id = nrConcreteEndanger.getId();
		String currentUserId = httpSessionService.getCurrentUserId();
		Date date = new Date();
		
		
		GasStationinfoEntity gasStationinfoEntity = gasStationinfoService.getById(id);
		gasStationinfoEntity.setName(nrConcreteEndanger.getName());
		gasStationinfoEntity.setTaxpayerCode(nrConcreteEndanger.getTaxpayerCode());
		gasStationinfoEntity.setTaxpayerName(nrConcreteEndanger.getTaxpayerName());
		gasStationinfoEntity.setTaxorgcode(nrConcreteEndanger.getTaxorgcode());
		
		gasStationinfoEntity.setUpdateTime(date);
		gasStationinfoEntity.setUpdateId(currentUserId);
		
		gasStationinfoService.updateById(gasStationinfoEntity);
		
		
		nrConcreteEndanger.setUpdateTime(date);
		nrConcreteEndanger.setUpdateId(currentUserId);
		
		nrConcreteEndangerService.updateById(nrConcreteEndanger);
		
		return DataResult.success();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据(运维)")
	@PostMapping("/list")
	@ResponseBody
	public DataResult list(@RequestBody GasStationinfoEntity vo) {
		String taxorgcode = vo.getTaxorgcode();

		String currentDeptId = httpSessionService.getCurrentDeptId();

		List<GasStationDeptEntity> list = gasStationDeptService.list();
		List<String> ids = new ArrayList<String>();
		for (GasStationDeptEntity gasStationDeptEntity : list) {
			String endangerId = gasStationDeptEntity.getEndangerId();
			String regionId = gasStationDeptEntity.getRegionId();
			ids.add(endangerId);
			ids.add(regionId);
		}

		List<String> _ids = ids.stream().distinct().collect(Collectors.toList());

		Page page = new Page(vo.getPage(), vo.getLimit());
		LambdaQueryWrapper<GasStationinfoEntity> queryWrapper = Wrappers.lambdaQuery();

		queryWrapper.eq(GasStationinfoEntity::getType, "group");
		if (TsBqUtils.isNotNull(taxorgcode)) {
			queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, taxorgcode);
		} else {

			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, currentDeptId);

			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.like(GasStationinfoEntity::getTaxorgcode, currentDeptId.substring(0, 5));

			} else {
				// 省局
			}
		}
		if (TsBqUtils.isNotNull(vo.getName())) {
			queryWrapper.like(GasStationinfoEntity::getName, vo.getName());
		}

		if (TsBqUtils.isNotNull(vo.getTaxpayerCode())) {
			queryWrapper.like(GasStationinfoEntity::getTaxpayerCode, vo.getTaxpayerCode());
		}

		queryWrapper.ne(GasStationinfoEntity::getPid, "0");
		queryWrapper.notIn(GasStationinfoEntity::getId, _ids);

		IPage<GasStationinfoEntity> iPage = gasStationinfoService.page(page, queryWrapper);

		List<GasStationinfoEntity> records = iPage.getRecords();

		for (GasStationinfoEntity gasStationinfoEntity : records) {
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}

			SysDept sysDept = deptService.getById(gasStationinfoEntity.getTaxorgcode());
			if (TsBqUtils.isNotNull(sysDept)) {
				String name = sysDept.getName();
				gasStationinfoEntity.setTaxorgName(name);

			}

			String id = gasStationinfoEntity.getId();
			NrConcreteEndangerEntity nrConcreteEndangerEntity = nrConcreteEndangerService.getById(id);
			if (TsBqUtils.isNotNull(nrConcreteEndangerEntity)) {
				gasStationinfoEntity.setIsAnalyseNrConcrete("Y");
				gasStationinfoEntity.setNrConcreteTon1(nrConcreteEndangerEntity.getTon1());
				
			} else {
				gasStationinfoEntity.setIsAnalyseNrConcrete("N");
			}

		}

		return DataResult.success(iPage);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/applyList1")
	@ResponseBody
	public DataResult applyList1(@RequestBody NrConcreteEndangerEntity nrConcreteEndanger) {
		
		String taxorgcode = nrConcreteEndanger.getTaxorgcode();
		String currentDeptId = httpSessionService.getCurrentDeptId();
		
		Page page = new Page(nrConcreteEndanger.getPage(), nrConcreteEndanger.getLimit());
		LambdaQueryWrapper<NrConcreteEndangerEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		if (TsBqUtils.isNotNull(taxorgcode)) {
			queryWrapper.eq(NrConcreteEndangerEntity::getTaxorgcode, taxorgcode);
		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(NrConcreteEndangerEntity::getTaxorgcode, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.like(NrConcreteEndangerEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}
		
		if (TsBqUtils.isNotNull(nrConcreteEndanger.getName())) {
			queryWrapper.like(NrConcreteEndangerEntity::getName, nrConcreteEndanger.getName());
		}

		if (TsBqUtils.isNotNull(nrConcreteEndanger.getTaxpayerCode())) {
			queryWrapper.eq(NrConcreteEndangerEntity::getTaxpayerCode, nrConcreteEndanger.getTaxpayerCode());
		}
		
		// queryWrapper.eq(NrConcreteEndangerEntity::getIndustry, "混凝土");
		
		queryWrapper.and((wrapper)->{
            wrapper.eq(NrConcreteEndangerEntity::getIndustry,"混凝土")
                    .or().isNull(NrConcreteEndangerEntity::getIndustry)
                    .or().eq(NrConcreteEndangerEntity::getIndustry, "");
        });
		
		IPage<NrConcreteEndangerEntity> iPage = nrConcreteEndangerService.page(page, queryWrapper);
		
		
		List<NrConcreteEndangerEntity> records = iPage.getRecords();
		
		for (NrConcreteEndangerEntity gasStationinfoEntity : records) {
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}

			SysDept sysDept = deptService.getById(gasStationinfoEntity.getTaxorgcode());
			if (TsBqUtils.isNotNull(sysDept)) {
				String name = sysDept.getName();
				gasStationinfoEntity.setTaxorgName(name);
			}
		}
		
		return DataResult.success(iPage);
	}
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/makeList1")
	@ResponseBody
	public DataResult makeList1(@RequestBody NrConcreteEndangerEntity nrConcreteEndanger) {
		
		String taxorgcode = nrConcreteEndanger.getTaxorgcode();
		String currentDeptId = httpSessionService.getCurrentDeptId();
		
		Page page = new Page(nrConcreteEndanger.getPage(), nrConcreteEndanger.getLimit());
		LambdaQueryWrapper<NrConcreteEndangerEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		if (TsBqUtils.isNotNull(taxorgcode)) {
			queryWrapper.eq(NrConcreteEndangerEntity::getTaxorgcode, taxorgcode);
		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(NrConcreteEndangerEntity::getTaxorgcode, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.like(NrConcreteEndangerEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}
		
		if (TsBqUtils.isNotNull(nrConcreteEndanger.getName())) {
			queryWrapper.like(NrConcreteEndangerEntity::getName, nrConcreteEndanger.getName());
		}

		if (TsBqUtils.isNotNull(nrConcreteEndanger.getTaxpayerCode())) {
			queryWrapper.eq(NrConcreteEndangerEntity::getTaxpayerCode, nrConcreteEndanger.getTaxpayerCode());
		}
		
		
		// queryWrapper.eq(NrConcreteEndangerEntity::getIndustry, "水泥制造");
		
		queryWrapper.and((wrapper)->{
            wrapper.eq(NrConcreteEndangerEntity::getIndustry,"水泥制造")
                    .or().isNull(NrConcreteEndangerEntity::getIndustry)
                    .or().eq(NrConcreteEndangerEntity::getIndustry, "");
        });
		
		IPage<NrConcreteEndangerEntity> iPage = nrConcreteEndangerService.page(page, queryWrapper);
		
		
		List<NrConcreteEndangerEntity> records = iPage.getRecords();
		
		for (NrConcreteEndangerEntity gasStationinfoEntity : records) {
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}

			SysDept sysDept = deptService.getById(gasStationinfoEntity.getTaxorgcode());
			if (TsBqUtils.isNotNull(sysDept)) {
				String name = sysDept.getName();
				gasStationinfoEntity.setTaxorgName(name);
			}
		}
		
		return DataResult.success(iPage);
	}
}
